package brand

import (
	"context"
	"mall/common/excption"
	constans "mall/services/product/rpc/consts"
	"mall/services/product/rpc/internal/domain/bo"
	"mall/services/product/rpc/internal/mapper"
	"mall/services/product/rpc/internal/mapper/entity"
	"mall/services/product/rpc/internal/svc"
	"mall/services/product/rpc/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type InfoLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBrandInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InfoLogic {
	return &InfoLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *InfoLogic) BrandInfo(in *pb.BrandInfoDTO) (*pb.BrandInfoVO, error) {
	// 1.query brand info
	brandMapper := mapper.NewBrandMapper(l.ctx)
	brandBO := bo.BrandBO{BrandId: in.BrandId, NeStatus: constans.Delete}
	brandEntity, err := brandMapper.SelectOneCtx(l.svcCtx.DBEngin, brandBO)
	if err != nil {
		return nil, err
	}
	if brandEntity.BrandId <= 0 {
		return nil, excption.NewErrCode(constans.BrandNoExistErr)
	}
	// 2.query category brand entity
	categoryVO, queryCategoryErr := l.queryCategoryBrand(in)
	if queryCategoryErr != nil {
		return nil, queryCategoryErr
	}
	// 3.return obj
	return l.packageObj(brandEntity, categoryVO), nil
}

func (l *InfoLogic) packageObj(brandEntity entity.BrandEntity, categoryVO *pb.CategoryVO) *pb.BrandInfoVO {
	resp := new(pb.BrandInfoVO)
	resp.BrandId = brandEntity.BrandId
	resp.CreateTime = brandEntity.CreateTime.Format("2006-01-02 15:04:05")
	resp.UpdateTime = brandEntity.UpdateTime.Format("2006-01-02 15:04:05")
	resp.Name = brandEntity.Name
	resp.Desc = brandEntity.Desc
	resp.ImgUrl = brandEntity.ImgUrl
	resp.FirstLetter = brandEntity.FirstLetter
	resp.Seq = int32(brandEntity.Seq)
	resp.Status = int32(brandEntity.Status)
	resp.Category = categoryVO
	return resp
}

func (l *InfoLogic) queryCategoryBrand(in *pb.BrandInfoDTO) (*pb.CategoryVO, error) {
	categoryBrandMapper := mapper.NewCategoryBrandMapper(l.ctx)
	categoryBrandBO := bo.CategoryBrandBO{}
	categoryBrandBO.BrandId = in.BrandId
	categoryVO := &pb.CategoryVO{}
	categoryBrandEntity, err := categoryBrandMapper.SelectOneCtx(l.svcCtx.DBEngin, categoryBrandBO)
	if err != nil {
		return nil, err
	}
	if categoryBrandEntity.CategoryId > 0 {
		categoryMapper := mapper.NewCategoryMapper(l.ctx)
		categoryEntity, categoryErr := categoryMapper.SelectOneCtx(l.svcCtx.DBEngin, bo.CategoryBO{
			CategoryId: categoryBrandEntity.CategoryId,
		})
		if categoryErr != nil {
			return nil, categoryErr
		}
		if categoryEntity.CategoryId > 0 {
			categoryVO.CategoryId = categoryEntity.CategoryId
			categoryVO.ShopId = categoryEntity.ShopId
			categoryVO.ParentId = categoryEntity.ParentId
			categoryVO.Name = categoryEntity.Name
			categoryVO.Desc = categoryEntity.Desc
			categoryVO.Path = categoryEntity.Path
			categoryVO.Status = int32(categoryEntity.Status)
			categoryVO.Icon = categoryEntity.Icon
			categoryVO.ImgUrl = categoryEntity.ImgUrl
			categoryVO.Level = int32(categoryEntity.Level)
			categoryVO.Status = int32(categoryEntity.Status)
		}
	}
	return categoryVO, nil
}
